package com.heatingcharge.service.sys.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heatingcharge.common.exception.BaseException;
import com.heatingcharge.common.util.ResponseCodeEnum;
import com.heatingcharge.dto.SysRoleDTO;
import com.heatingcharge.entity.sys.SysRole;
import com.heatingcharge.entity.sys.SysUser;
import com.heatingcharge.mapper.sys.SysRoleMapper;
import com.heatingcharge.param.SysRoleParam;
import com.heatingcharge.security.JwtUser;
import com.heatingcharge.security.util.SecurityUtil;
import com.heatingcharge.service.sys.ISysRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 角色  服务实现类
 * </p>
 *
 * @author devin.jiang
 * @since 2019-06-27
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {

    @Override
    public List<SysRole> getRoleList(SysRole sysRole) {
        return baseMapper.selectList(null);
    }

    @Override
    public boolean save(SysRoleParam sysRoleParam) {
        if (checkExist(sysRoleParam.getName(),null)){
            throw new BaseException(ResponseCodeEnum.ROLE_EXIST.getMessage(),ResponseCodeEnum.ROLE_EXIST.getCode());
        }
        JwtUser user = SecurityUtil.getUser();
        SysRole sysRole=new SysRole();
        BeanUtils.copyProperties(sysRoleParam,sysRole);
        sysRole.setCreatedTime(new Date());
        sysRole.setCreatedBy(user.getRealName());
        return baseMapper.insert(sysRole)>0;
    }
    private boolean checkExist(String name,Integer id){
        QueryWrapper<SysRole> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("name",name);
        if (id!=null){
            queryWrapper.ne("id",id);
        }
        return this.list(queryWrapper).size()>0;
    }
    @Override
    public boolean update(SysRoleParam sysRoleParam) {
        if (checkExist(sysRoleParam.getName(),sysRoleParam.getId())){
            throw new BaseException(ResponseCodeEnum.ROLE_EXIST.getMessage(),ResponseCodeEnum.ROLE_EXIST.getCode());
        }
        JwtUser user = SecurityUtil.getUser();
        SysRole sysRole=new SysRole();
        BeanUtils.copyProperties(sysRoleParam,sysRole);
        sysRole.setUpdatedTime(new Date());
        sysRole.setUpdatedBy(user.getRealName());
        return baseMapper.insert(sysRole)>0;
    }

    @Override
    public IPage<SysRole> getSysRoles(Page page, SysRoleDTO sysRoleDTO) {
        QueryWrapper<SysRole> queryWrapper=new QueryWrapper<>();
        if (StrUtil.isNotEmpty(sysRoleDTO.getName())){
            queryWrapper.like("name",sysRoleDTO.getName());
        }
        return baseMapper.selectPage(page,queryWrapper);
    }
}
